package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.basic.service.BoardService;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.model.HInventoryModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ExportExcelUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

/**
 * @description:库存
 * @author: th
 * @time: 2020/3/23 14:00
 */
@Service
@Log4j2
public class HInventoryService {

    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HBasicService HBasicService;
    @Autowired
    private LogService logService;
    @Autowired
    private BoardService boardService;

    @Value("${spring.profiles.active}")
    private String active;

    @Value("${user.upload.path}")
    private String uploadPath;

    /**
     * 入库保存库存信息
     * @param hOrderDetailModel
     * @return
     */
    public HOrderDetailModel saveHInventory(HOrderDetailModel hOrderDetailModel){

        //封装库存信息
        HInventory hInventory = new HInventory();
        hInventory.setItyMatCode("111111111");//系统物料编码
        hInventory.setItyMatName(hOrderDetailModel.getOdaMatName());//物料描述
        if(hOrderDetailModel.getOdaProCode() != null) {
            hInventory.setItyProCode(boardService.projectCodeHandle(hOrderDetailModel.getOdaProCode()));//项目编号
        }
        hInventory.setItyProName(hOrderDetailModel.getOdaProName());//项目名称
        hInventory.setItyPart(hOrderDetailModel.getOdaPart());//部位
        hInventory.setItyNum(hOrderDetailModel.getOdaAvaNum());//在库数量
        hInventory.setItyTrackCode(hOrderDetailModel.getOdaTrackCode());//跟踪号
        hInventory.setItyAvaNum(hOrderDetailModel.getOdaAvaNum());//可用数量
        hInventory.setItyUnit(hOrderDetailModel.getOdaUnit());//单位
        hInventory.setItyPrice(hOrderDetailModel.getOdaPrice());//单价
        hInventory.setItyBrand(hOrderDetailModel.getOdaBrand());//品牌
        hInventory.setItyPosition(hOrderDetailModel.getOdaPosition());//区位
        if(("外购件").equals(hOrderDetailModel.getOdaMatTypeName())){
            hInventory.setItyMatType(1);
        }else if(("外协件").equals(hOrderDetailModel.getOdaMatTypeName())){
            hInventory.setItyMatType(2);
        }else {
            hInventory.setItyMatType(hOrderDetailModel.getOdaMatType());
        }
        if(hOrderDetailModel.getOdaWhiId() == null) {
            hInventory.setItyWhiId(HBasicService.getWarIdByName(hOrderDetailModel.getOdaWhiIdName()));
        }else {
            hInventory.setItyWhiId(hOrderDetailModel.getOdaWhiId());
        }
        if(("库存在库").equals(hOrderDetailModel.getOdaStockTypeName())){
            hInventory.setItyStockType(1);//在库类型 1是库存在库，2是项目在库
        }else if(("项目在库").equals(hOrderDetailModel.getOdaStockTypeName())){
            hInventory.setItyStockType(2);
        }else {
            hInventory.setItyStockType(hOrderDetailModel.getOdaStockType());
        }
        if(("成品件").equals(hOrderDetailModel.getOdaOutsourcedTypeName())){
            hInventory.setItyOutsourcedType(1);
        }else if(("毛坯件").equals(hOrderDetailModel.getOdaOutsourcedTypeName())){
            hInventory.setItyOutsourcedType(2);
        }else {
            hInventory.setItyOutsourcedType(hOrderDetailModel.getOdaOutsourcedType());
        }
        hInventory.setItyRowNo(hOrderDetailModel.getOdaId());//入库明细ID
        hInventory.setItySapCode(hOrderDetailModel.getOdaSapCode());//SAP物料编码
        hInventory.setItyRemark(hOrderDetailModel.getOdaRemark());//备注
        hInventory.setItyDrawingNumber(hOrderDetailModel.getOdaDrawingCode());//图号
        hInventory.setItyMakingPiecesStatus(hOrderDetailModel.getOdaMakingPiecesStatus());//存放状态
        hInventory.setItyCaseNo(hOrderDetailModel.getOdaCaseNo());//箱号
        hInventory.setItyBoxingDate(hOrderDetailModel.getOdaBoxingDate());//装箱日期
        hInventory.setItyStorageDate(hOrderDetailModel.getOdaStorageDate());//入库日期
        if (("是").equals(hOrderDetailModel.getOdaIfFinishedProductName())) {
            hInventory.setItyIfFinishedProduct(1);
        } else if (("否").equals(hOrderDetailModel.getOdaIfFinishedProductName())) {
            hInventory.setItyIfFinishedProduct(0);
        } else {
            hInventory.setItyIfFinishedProduct(null);
        }//是否成品
        hInventory.setItyWeight(hOrderDetailModel.getOdaWeight());
        hInventory.setItyAvaWeight(hOrderDetailModel.getOdaWeight());
        hInventory.setItyOrderNum(hOrderDetailModel.getOdaOrderNum());
        hInventory.setItyTexture(hOrderDetailModel.getOdaTexture());
        hInventory.setItySpec(hOrderDetailModel.getOdaSpec());
        hInventory.setOdaSupplier(hOrderDetailModel.getOdaSupplier());
        hInventory.setContractNo(hOrderDetailModel.getContractNo());

        //查询是否有同种物料
        HInventory model = hInventoryDao.selectAllByName(hInventory);
        if(model != null){
            hInventoryDao.update(hInventory);
            hOrderDetailModel.setItyId(hInventory.getItyId());
        }else {
            hInventoryDao.insert(hInventory);
            hOrderDetailModel.setItyId(hInventory.getItyId());
        }
        return hOrderDetailModel;
    }

    /**
     * 出库保存库存信息
     * @param hOrderDetailModel
     * @return
     */
    public HOrderDetailModel saveOutHInventory(HOrderDetailModel hOrderDetailModel) {

        //封装库存信息
        HInventory hInventory = new HInventory();
        hInventory.setItyAvaNum(hOrderDetailModel.getOdaAvaNum());//出库数量
        hInventory.setItyId(hOrderDetailModel.getItyId());
        hInventory.setItyAvaWeight(hOrderDetailModel.getOdaWeight());
        hInventoryDao.updateHInventoryById(hInventory);
        return hOrderDetailModel;
    }


    /**
     * 调拨时保存出库库存
     * @param hOrderDetailModel
     * @return
     */
    public HOrderDetailModel saveAllocationHInventory(HOrderDetailModel hOrderDetailModel) {

        //封装库存信息
        HInventory hInventory = new HInventory();
        hInventory.setItySapCode(hOrderDetailModel.getOdaSapCode());
        hInventory.setItyMatName(hOrderDetailModel.getOdaMatName());
        hInventory.setItyNum(hOrderDetailModel.getOdaAvaNum());
        hInventory.setItyUnit(hOrderDetailModel.getOdaUnit());

        //按物料描述，物料名称，数量，单位进行匹配出库
        hInventoryDao.updateHInventoryByDesc(hInventory);
        return hOrderDetailModel;
    }

    /**
     * 根据库存ID查询库存
     * @param id
     * @return
     */
    public List<HInventoryModel> selectInvByItyId(Integer id) {

        return hInventoryDao.selectInvByItyId(id);
    }

    /**
     * 根据库存ID修改库存
     * @param hInventoryModel
     * @return
     */
    public int updateInvByItyId(HInventoryModel hInventoryModel) {

        logService.saveLog("【成品库】库存信息编辑","修改" + hInventoryModel.getItyMatName() + "的库存信息");
        return hInventoryDao.updateInvByItyId(hInventoryModel);
    }

    /**
     * 库存查询
     * @param hInventoryQueryModel
     * @return
     */
    public List<HInventoryModel> selectInventoryByQuery(HInventoryQueryModel hInventoryQueryModel) {

        return hInventoryDao.selectHInventoryModeByQuery(hInventoryQueryModel);
    }

    /**
     * 库存批量修改
     * @param param
     * @return
     */
    @Transactional
    public boolean invCommits(HInventoryModel param) {
        try {
            hInventoryDao.updateSelectiveByPrimaryKeys(param);
            logService.saveLog("【成品库】库存信息编辑","批量修改库存信息");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return false;
        }
        return true;
    }

//    /**
//     * 库存导出
//     * @param response
//     * @param param
//     */
//    public void exportExcel(HttpServletResponse response, HInventoryQueryModel param) {
//
//        List<HInventoryModel> model = hInventoryDao.selectHInventoryModeByQuery(param);
//        Subject userSubject = ShiroUtils.getSubject();
//        // 创建HSSFWorkbook对象(excel的文档对象)
//        HSSFWorkbook wb = new HSSFWorkbook();
//        // 建立新的sheet对象（excel的表单）
//        HSSFSheet hssfSheet = wb.createSheet("sheet1");
//        // 在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
//        HSSFRow hssfRow = hssfSheet.createRow(0);
//        // 创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
//        HSSFCell hssfCell = hssfRow.createCell(0);
//        // 合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
////        hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 14));
//        // 设置单元格内容
////        hssfCell.setCellValue("导出库存数据" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
////        hssfRow = hssfSheet.createRow(1);
//        if(param.getItyMatType() == 3){
//            hssfRow.createCell(0).setCellValue("序号");
//            hssfRow.createCell(1).setCellValue("物料分类");
//            hssfRow.createCell(2).setCellValue("物料描述");
//            hssfRow.createCell(3).setCellValue("图号");
//            hssfRow.createCell(4).setCellValue("数量");
//            hssfRow.createCell(5).setCellValue("单位");
//            hssfRow.createCell(6).setCellValue("项目编号");
//            hssfRow.createCell(7).setCellValue("项目名称");
//            hssfRow.createCell(8).setCellValue("部分名称");
//            hssfRow.createCell(9).setCellValue("是否成品");
//            hssfRow.createCell(10).setCellValue("存放状态");
//            hssfRow.createCell(11).setCellValue("箱号");
//            hssfRow.createCell(12).setCellValue("装箱日期");
//            hssfRow.createCell(13).setCellValue("库位");
//            hssfRow.createCell(14).setCellValue("区位");
//            hssfRow.createCell(15).setCellValue("备注");
//            hssfRow.createCell(16).setCellValue("在库类型");
//            hssfRow.createCell(17).setCellValue("记账日期");
//
//            // 在sheet里创建第二行
//            for (int i = 0; i < model.size(); i++) {
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                HSSFRow row = hssfSheet.createRow(i + 1);
//                row.createCell(0).setCellValue(i+1);
//                row.createCell(1).setCellValue(model.get(i).getItyMatType()==2?"外协件":model.get(i).getItyMatType()==3?"制作件":model.get(i).getItyMatType()==4?"铝料":"");
//                row.createCell(2).setCellValue(model.get(i).getItyMatName());
//                row.createCell(3).setCellValue(model.get(i).getItyDrawingNumber());
//                row.createCell(4).setCellValue(model.get(i).getItyNum());
//                row.createCell(5).setCellValue(model.get(i).getItyUnit());
//                row.createCell(6).setCellValue(model.get(i).getItyProCode());
//                row.createCell(7).setCellValue(model.get(i).getItyProName());
//                row.createCell(8).setCellValue(model.get(i).getItyPart());
//                row.createCell(9).setCellValue(model.get(i).getItyIfFinishedProduct()==1?"是":"否");
//                row.createCell(10).setCellValue(model.get(i).getItyMakingPiecesStatus());
//                row.createCell(11).setCellValue(model.get(i).getItyCaseNo());
//                if(model.get(i).getItyBoxingDate()!=null){
//                    row.createCell(12).setCellValue(simpleDateFormat.format(model.get(i).getItyBoxingDate()));
//                }
//                row.createCell(13).setCellValue(model.get(i).getItyWhiName());
//                row.createCell(14).setCellValue(model.get(i).getItyPosition());
//                row.createCell(15).setCellValue(model.get(i).getItyRemark());
//                row.createCell(16).setCellValue(model.get(i).getItyStockType()==1?"库存在库":"项目在库");
//                if(model.get(i).getItyStorageDate()!=null){
//                    row.createCell(17).setCellValue(simpleDateFormat.format(model.get(i).getItyStorageDate()));
//                }
//            }
//        } else if(param.getItyMatType() == 2){
//            hssfRow.createCell(0).setCellValue("序号");
//            hssfRow.createCell(1).setCellValue("物料分类");
//            hssfRow.createCell(2).setCellValue("SAP物料编码");
//            hssfRow.createCell(3).setCellValue("物料描述");
//            hssfRow.createCell(4).setCellValue("图号");
//            hssfRow.createCell(5).setCellValue("数量");
//            hssfRow.createCell(6).setCellValue("单位");
//            hssfRow.createCell(7).setCellValue("单价");
//            hssfRow.createCell(8).setCellValue("金额");
//            hssfRow.createCell(9).setCellValue("跟踪号");
//            hssfRow.createCell(10).setCellValue("项目编号");
//            hssfRow.createCell(11).setCellValue("项目名称");
//            hssfRow.createCell(12).setCellValue("部分名称");
//            hssfRow.createCell(13).setCellValue("厂家");
//            hssfRow.createCell(14).setCellValue("外协件类别");
//            hssfRow.createCell(15).setCellValue("区位");
//            hssfRow.createCell(16).setCellValue("备注");
//            hssfRow.createCell(17).setCellValue("在库类型");
//            hssfRow.createCell(18).setCellValue("记账日期");
//
//            // 在sheet里创建第二行
//            for (int i = 0; i < model.size(); i++) {
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                HSSFRow row = hssfSheet.createRow(i + 1);
//                row.createCell(0).setCellValue(i+1);
//                row.createCell(1).setCellValue(model.get(i).getItyMatType()==2?"外协件":model.get(i).getItyMatType()==3?"制作件":model.get(i).getItyMatType()==4?"铝料":"");
//                row.createCell(2).setCellValue(model.get(i).getItySapCode());
//                row.createCell(3).setCellValue(model.get(i).getItyMatName());
//                row.createCell(4).setCellValue(model.get(i).getItyDrawingNumber());
//                row.createCell(5).setCellValue(model.get(i).getItyNum());
//                row.createCell(6).setCellValue(model.get(i).getItyUnit());
//                if(userSubject.isPermitted("hhalinv:amount:view")){
//                    row.createCell(7).setCellValue(model.get(i).getItyPrice());
//                    row.createCell(8).setCellValue(model.get(i).getItyAmount());
//                }else {
//                    row.createCell(7).setCellValue("**");
//                    row.createCell(8).setCellValue("**");
//                }
//                row.createCell(9).setCellValue(model.get(i).getItyTrackCode());
//                row.createCell(10).setCellValue(model.get(i).getItyProCode());
//                row.createCell(11).setCellValue(model.get(i).getItyProName());
//                row.createCell(12).setCellValue(model.get(i).getItyPart());
//                row.createCell(13).setCellValue(model.get(i).getItyBrand());
//                row.createCell(14).setCellValue(model.get(i).getItyOutsourcedType()==1?"成品件":"毛坯件");
//                row.createCell(15).setCellValue(model.get(i).getItyPosition());
//                row.createCell(16).setCellValue(model.get(i).getItyRemark());
//                row.createCell(17).setCellValue(model.get(i).getItyStockType()==1?"库存在库":"项目在库");
//                if(model.get(i).getItyStorageDate()!=null){
//                    row.createCell(18).setCellValue(simpleDateFormat.format(model.get(i).getItyStorageDate()));
//                }
//            }
//        } else if(param.getItyMatType() == 4){
//            hssfRow.createCell(0).setCellValue("序号");
//            hssfRow.createCell(1).setCellValue("物料分类");
//            hssfRow.createCell(2).setCellValue("类型");
//            hssfRow.createCell(3).setCellValue("材质");
//            hssfRow.createCell(4).setCellValue("规格");
//            hssfRow.createCell(5).setCellValue("模具号");
//            hssfRow.createCell(6).setCellValue("数量");
//            hssfRow.createCell(7).setCellValue("总重量");
//            hssfRow.createCell(8).setCellValue("单价");
//            hssfRow.createCell(9).setCellValue("金额");
//            hssfRow.createCell(10).setCellValue("跟踪号");
//            hssfRow.createCell(11).setCellValue("订单号");
//            hssfRow.createCell(12).setCellValue("项目编号");
//            hssfRow.createCell(13).setCellValue("项目名称");
//            hssfRow.createCell(14).setCellValue("区位");
//            hssfRow.createCell(15).setCellValue("备注");
//            hssfRow.createCell(16).setCellValue("在库类型");
//            hssfRow.createCell(17).setCellValue("记账日期");
//            // 在sheet里创建第二行
//            for (int i = 0; i < model.size(); i++) {
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                HSSFRow row = hssfSheet.createRow(i + 1);
//                row.createCell(0).setCellValue(i+1);
//                row.createCell(1).setCellValue(model.get(i).getItyMatType()==2?"外协件":model.get(i).getItyMatType()==3?"制作件":model.get(i).getItyMatType()==4?"铝料":"");
//                row.createCell(2).setCellValue(model.get(i).getItyMatName());
//                row.createCell(3).setCellValue(model.get(i).getItyTexture());
//                row.createCell(4).setCellValue(model.get(i).getItySpec());
//                row.createCell(5).setCellValue(model.get(i).getItyDrawingNumber());
//                row.createCell(6).setCellValue(model.get(i).getItyNum());
//                row.createCell(7).setCellValue(model.get(i).getItyWeight());
//                if(userSubject.isPermitted("hhalinv:amount:view")){
//                    row.createCell(8).setCellValue(model.get(i).getItyPrice());
//                    row.createCell(9).setCellValue(model.get(i).getItyAmount());
//                }else {
//                    row.createCell(8).setCellValue("**");
//                    row.createCell(9).setCellValue("**");
//                }
//                row.createCell(10).setCellValue(model.get(i).getItyTrackCode());
//                row.createCell(11).setCellValue(model.get(i).getItyOrderNum());
//                row.createCell(12).setCellValue(model.get(i).getItyProCode());
//                row.createCell(13).setCellValue(model.get(i).getItyProName());
//                row.createCell(14).setCellValue(model.get(i).getItyPosition());
//                row.createCell(15).setCellValue(model.get(i).getItyRemark());
//                row.createCell(16).setCellValue(model.get(i).getItyStockType()==1?"库存在库":"项目在库");
//                if(model.get(i).getItyStorageDate()!=null){
//                    row.createCell(17).setCellValue(simpleDateFormat.format(model.get(i).getItyStorageDate()));
//                }
//            }
//        }
//
//        //输出Excel文件
//        try {
//            OutputStream output = response.getOutputStream();
//            response.reset();
//            response.setHeader("Content-disposition", "attachment; filename=" + new String( "成品库库存信息.xls".getBytes("gb2312"), "ISO8859-1" ));
//            response.setContentType("application/msexcel");
//            wb.write(output);
//            output.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    /**
     * 库存导出
     * @param param
     */
    public JsonResult exportExcel(HInventoryQueryModel param) {
        JsonResult jsonResult = new JsonResult();
        //获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        logService.saveLog("成品库库存查询——导出","用户:【"+user.getName()+"】执行导出操作！");
        //存储/读取Excel的文件夹的路径 mms文件夹下/用户账户/
        String path = uploadPath + user.getAccount() + "/";
        String excelFileName = "成品库库存信息.xlsx";
        try {
            //1.创建临时内存表名
            String tempTableName = "##" + user.getAccount() + (UUID.randomUUID().toString().replace("-", ""));
            param.setTempTableName(tempTableName);
            Subject userSubject = ShiroUtils.getSubject();
            //2.查询出数据导入到临时表
            //制作件
            if (param.getItyMatType() == 3) {
                if(userSubject.isPermitted("hhalinv:amount:view")) {
                    hInventoryDao.createFabricatedPartsExportByQueryTempTable(param);
                }else{
                    hInventoryDao.createFabricatedPartsExportByQueryNotMoneyTempTable(param);
                }
            }
            //外协件
            else  if (param.getItyMatType() == 2) {
                if(userSubject.isPermitted("hhalinv:amount:view")) {
                    hInventoryDao.createOutsourcedPartsExportByQueryTempTable(param);
                }else{
                    hInventoryDao.createOutsourcedPartsExportByQueryNotMoneyTempTable(param);
                }
            }
            //铝料
            else  if (param.getItyMatType() == 4) {
                if(userSubject.isPermitted("hhalinv:amount:view")) {
                    hInventoryDao.createAluminumMaterialExportByQueryTempTable(param);
                }else{
                    hInventoryDao.createAluminumMaterialExportByQueryNotNoneyTempTable(param);
                }
            }
            //3.导出Excel
            if(ExportExcelUtils.BuildExcel(path, excelFileName, tempTableName, active,true)){
                jsonResult.setData(excelFileName);
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
            } else {
                jsonResult.setType(JsonResult.ResultType.ERROR);
            }
        } catch (Exception e) {
            log.debug(e.getMessage());
            e.printStackTrace();
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        return  jsonResult;
//        List<HInventoryModel> model = hInventoryDao.selectHInventoryModeByQuery(param);
//        Subject userSubject = ShiroUtils.getSubject();
//        // 创建HSSFWorkbook对象(excel的文档对象)
//        HSSFWorkbook wb = new HSSFWorkbook();
//        // 建立新的sheet对象（excel的表单）
//        HSSFSheet hssfSheet = wb.createSheet("sheet1");
//        // 在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
//        HSSFRow hssfRow = hssfSheet.createRow(0);
//        // 创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
//        HSSFCell hssfCell = hssfRow.createCell(0);
//        // 合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
////        hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 14));
//        // 设置单元格内容
////        hssfCell.setCellValue("导出库存数据" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
////        hssfRow = hssfSheet.createRow(1);
//        if(param.getItyMatType() == 3){
//            hssfRow.createCell(0).setCellValue("序号");
//            hssfRow.createCell(1).setCellValue("物料分类");
//            hssfRow.createCell(2).setCellValue("物料描述");
//            hssfRow.createCell(3).setCellValue("图号");
//            hssfRow.createCell(4).setCellValue("数量");
//            hssfRow.createCell(5).setCellValue("单位");
//            hssfRow.createCell(6).setCellValue("项目编号");
//            hssfRow.createCell(7).setCellValue("项目名称");
//            hssfRow.createCell(8).setCellValue("部分名称");
//            hssfRow.createCell(9).setCellValue("是否成品");
//            hssfRow.createCell(10).setCellValue("存放状态");
//            hssfRow.createCell(11).setCellValue("箱号");
//            hssfRow.createCell(12).setCellValue("装箱日期");
//            hssfRow.createCell(13).setCellValue("库位");
//            hssfRow.createCell(14).setCellValue("区位");
//            hssfRow.createCell(15).setCellValue("备注");
//            hssfRow.createCell(16).setCellValue("在库类型");
//            hssfRow.createCell(17).setCellValue("记账日期");
//
//            // 在sheet里创建第二行
//            for (int i = 0; i < model.size(); i++) {
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                HSSFRow row = hssfSheet.createRow(i + 1);
//                row.createCell(0).setCellValue(i+1);
//                row.createCell(1).setCellValue(model.get(i).getItyMatType()==2?"外协件":model.get(i).getItyMatType()==3?"制作件":model.get(i).getItyMatType()==4?"铝料":"");
//                row.createCell(2).setCellValue(model.get(i).getItyMatName());
//                row.createCell(3).setCellValue(model.get(i).getItyDrawingNumber());
//                row.createCell(4).setCellValue(model.get(i).getItyNum());
//                row.createCell(5).setCellValue(model.get(i).getItyUnit());
//                row.createCell(6).setCellValue(model.get(i).getItyProCode());
//                row.createCell(7).setCellValue(model.get(i).getItyProName());
//                row.createCell(8).setCellValue(model.get(i).getItyPart());
//                row.createCell(9).setCellValue(model.get(i).getItyIfFinishedProduct()==1?"是":"否");
//                row.createCell(10).setCellValue(model.get(i).getItyMakingPiecesStatus());
//                row.createCell(11).setCellValue(model.get(i).getItyCaseNo());
//                if(model.get(i).getItyBoxingDate()!=null){
//                    row.createCell(12).setCellValue(simpleDateFormat.format(model.get(i).getItyBoxingDate()));
//                }
//                row.createCell(13).setCellValue(model.get(i).getItyWhiName());
//                row.createCell(14).setCellValue(model.get(i).getItyPosition());
//                row.createCell(15).setCellValue(model.get(i).getItyRemark());
//                row.createCell(16).setCellValue(model.get(i).getItyStockType()==1?"库存在库":"项目在库");
//                if(model.get(i).getItyStorageDate()!=null){
//                    row.createCell(17).setCellValue(simpleDateFormat.format(model.get(i).getItyStorageDate()));
//                }
//            }
//        } else if(param.getItyMatType() == 2){
//            hssfRow.createCell(0).setCellValue("序号");
//            hssfRow.createCell(1).setCellValue("物料分类");
//            hssfRow.createCell(2).setCellValue("SAP物料编码");
//            hssfRow.createCell(3).setCellValue("物料描述");
//            hssfRow.createCell(4).setCellValue("图号");
//            hssfRow.createCell(5).setCellValue("数量");
//            hssfRow.createCell(6).setCellValue("单位");
//            hssfRow.createCell(7).setCellValue("单价");
//            hssfRow.createCell(8).setCellValue("金额");
//            hssfRow.createCell(9).setCellValue("跟踪号");
//            hssfRow.createCell(10).setCellValue("项目编号");
//            hssfRow.createCell(11).setCellValue("项目名称");
//            hssfRow.createCell(12).setCellValue("部分名称");
//            hssfRow.createCell(13).setCellValue("厂家");
//            hssfRow.createCell(14).setCellValue("外协件类别");
//            hssfRow.createCell(15).setCellValue("区位");
//            hssfRow.createCell(16).setCellValue("备注");
//            hssfRow.createCell(17).setCellValue("在库类型");
//            hssfRow.createCell(18).setCellValue("记账日期");
//
//            // 在sheet里创建第二行
//            for (int i = 0; i < model.size(); i++) {
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                HSSFRow row = hssfSheet.createRow(i + 1);
//                row.createCell(0).setCellValue(i+1);
//                row.createCell(1).setCellValue(model.get(i).getItyMatType()==2?"外协件":model.get(i).getItyMatType()==3?"制作件":model.get(i).getItyMatType()==4?"铝料":"");
//                row.createCell(2).setCellValue(model.get(i).getItySapCode());
//                row.createCell(3).setCellValue(model.get(i).getItyMatName());
//                row.createCell(4).setCellValue(model.get(i).getItyDrawingNumber());
//                row.createCell(5).setCellValue(model.get(i).getItyNum());
//                row.createCell(6).setCellValue(model.get(i).getItyUnit());
//                if(userSubject.isPermitted("hhalinv:amount:view")){
//                    row.createCell(7).setCellValue(model.get(i).getItyPrice());
//                    row.createCell(8).setCellValue(model.get(i).getItyAmount());
//                }else {
//                    row.createCell(7).setCellValue("**");
//                    row.createCell(8).setCellValue("**");
//                }
//                row.createCell(9).setCellValue(model.get(i).getItyTrackCode());
//                row.createCell(10).setCellValue(model.get(i).getItyProCode());
//                row.createCell(11).setCellValue(model.get(i).getItyProName());
//                row.createCell(12).setCellValue(model.get(i).getItyPart());
//                row.createCell(13).setCellValue(model.get(i).getItyBrand());
//                row.createCell(14).setCellValue(model.get(i).getItyOutsourcedType()==1?"成品件":"毛坯件");
//                row.createCell(15).setCellValue(model.get(i).getItyPosition());
//                row.createCell(16).setCellValue(model.get(i).getItyRemark());
//                row.createCell(17).setCellValue(model.get(i).getItyStockType()==1?"库存在库":"项目在库");
//                if(model.get(i).getItyStorageDate()!=null){
//                    row.createCell(18).setCellValue(simpleDateFormat.format(model.get(i).getItyStorageDate()));
//                }
//            }
//        } else if(param.getItyMatType() == 4){
//            hssfRow.createCell(0).setCellValue("序号");
//            hssfRow.createCell(1).setCellValue("物料分类");
//            hssfRow.createCell(2).setCellValue("类型");
//            hssfRow.createCell(3).setCellValue("材质");
//            hssfRow.createCell(4).setCellValue("规格");
//            hssfRow.createCell(5).setCellValue("模具号");
//            hssfRow.createCell(6).setCellValue("数量");
//            hssfRow.createCell(7).setCellValue("总重量");
//            hssfRow.createCell(8).setCellValue("单价");
//            hssfRow.createCell(9).setCellValue("金额");
//            hssfRow.createCell(10).setCellValue("跟踪号");
//            hssfRow.createCell(11).setCellValue("订单号");
//            hssfRow.createCell(12).setCellValue("项目编号");
//            hssfRow.createCell(13).setCellValue("项目名称");
//            hssfRow.createCell(14).setCellValue("区位");
//            hssfRow.createCell(15).setCellValue("备注");
//            hssfRow.createCell(16).setCellValue("在库类型");
//            hssfRow.createCell(17).setCellValue("记账日期");
//            // 在sheet里创建第二行
//            for (int i = 0; i < model.size(); i++) {
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                HSSFRow row = hssfSheet.createRow(i + 1);
//                row.createCell(0).setCellValue(i+1);
//                row.createCell(1).setCellValue(model.get(i).getItyMatType()==2?"外协件":model.get(i).getItyMatType()==3?"制作件":model.get(i).getItyMatType()==4?"铝料":"");
//                row.createCell(2).setCellValue(model.get(i).getItyMatName());
//                row.createCell(3).setCellValue(model.get(i).getItyTexture());
//                row.createCell(4).setCellValue(model.get(i).getItySpec());
//                row.createCell(5).setCellValue(model.get(i).getItyDrawingNumber());
//                row.createCell(6).setCellValue(model.get(i).getItyNum());
//                row.createCell(7).setCellValue(model.get(i).getItyWeight());
//                if(userSubject.isPermitted("hhalinv:amount:view")){
//                    row.createCell(8).setCellValue(model.get(i).getItyPrice());
//                    row.createCell(9).setCellValue(model.get(i).getItyAmount());
//                }else {
//                    row.createCell(8).setCellValue("**");
//                    row.createCell(9).setCellValue("**");
//                }
//                row.createCell(10).setCellValue(model.get(i).getItyTrackCode());
//                row.createCell(11).setCellValue(model.get(i).getItyOrderNum());
//                row.createCell(12).setCellValue(model.get(i).getItyProCode());
//                row.createCell(13).setCellValue(model.get(i).getItyProName());
//                row.createCell(14).setCellValue(model.get(i).getItyPosition());
//                row.createCell(15).setCellValue(model.get(i).getItyRemark());
//                row.createCell(16).setCellValue(model.get(i).getItyStockType()==1?"库存在库":"项目在库");
//                if(model.get(i).getItyStorageDate()!=null){
//                    row.createCell(17).setCellValue(simpleDateFormat.format(model.get(i).getItyStorageDate()));
//                }
//            }
//        }
//
//        //输出Excel文件
//        try {
//            OutputStream output = response.getOutputStream();
//            response.reset();
//            response.setHeader("Content-disposition", "attachment; filename=" + new String( "成品库库存信息.xls".getBytes("gb2312"), "ISO8859-1" ));
//            response.setContentType("application/msexcel");
//            wb.write(output);
//            output.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
